/*
 * GraphicalManager.h
 *
 *  Created on: Nov 2, 2008
 *      Author: tfannes
 */

#ifndef GRAPHICALMANAGER_H_
#define GRAPHICALMANAGER_H_

#include "../model/TileDescription.h"
#include "../model/CornerBuildingDescription.h"
#include "../model/SideBuildingDescription.h"
#include "../model/HexGrid.h"
#include <QPixmap>
#include <QMap>

using namespace structure;

class LayoutManager
{
public:
	// constructor & deconstructor
	LayoutManager(const QString & tileFile);
	virtual ~LayoutManager();

	// pixmap functions
	void getTilePixmap(const board::TileData & data, QPixmap & pixmapToFill) const;
	void getTileOverlay(QPixmap & pixmapToFill) const;
	QPixmap getTilePixmap(const board::TileData & data) const;
	QPixmap getTileOverlay() const;
	void getCornerPixmap(const board::CornerBuildingData & data, QPixmap & pixmapToFill) const;
	void getCornerOverlay(QPixmap & pixmapToFill) const;
	QPixmap getCornerPixmap(const board::CornerBuildingData & data) const;
	QPixmap getCornerOverlay() const;
	void getSidePixmap(const board::SideBuildingData & data, const structure::HexVector & position, QPixmap & pixmapToFill) const;
	void getSideOverlay(const structure::HexVector & position, QPixmap & pixmapToFill) const;
	QPixmap getSidePixmap(const board::SideBuildingData & data, const structure::HexVector & position) const;
	QPixmap getSideOverlay(const structure::HexVector & position) const;

	// hexvector to position functions
	QRect getElementRect(const structure::HexVector & position, const QSize & windowSize) const;
	void getElementRect(const structure::HexVector & position, const QSize & windowSize, QRect & rect) const;
	QPoint getElementCenter(const structure::HexVector & position, const QSize & windowSize) const;
	void getElementCenter(const structure::HexVector & position, const QSize & windowSize, QPoint & point) const;

	// position to hexvector functions
	HexVector getNearestTile(const QPoint & point, const QSize & size) const;
	HexVector getNearestCorner(const QPoint & point, const QSize & size) const;
	HexVector getNearestSide(const QPoint & point, const QSize & size) const;

	// position check functions
	bool isInTile(const structure::HexVector & position, const QSize & windowSize, const QPoint & point) const;
	bool isInCorner(const structure::HexVector & position, const QSize & windowSize, const QPoint & point) const;
	bool isInSide(const structure::HexVector & position, const QSize & windowSize, const QPoint & point) const;
	bool isInElement(const structure::HexVector & position, const QSize & windowSize, const QPoint & point) const;

	// initialization functions
	bool initializeTiles(const QString & filename);

	// getter functions
	inline const QSize & getTileSize() const { return tileSize; };
	inline const QSize & getGridSize() const { return gridSize; };
	inline const QSize & getCornerSize() const { return cornerSize; };
	inline const QSize & getSideSize(structure::SideType sideType) const { return sideSize[sideType]; };

private:
	void setLayoutData(const QSize & newTileSize);

private:
	QMap<board::TileType, QMap<board::TileResource, QPixmap> > arTileResource;
	QMap<board::TileNumber, QPixmap> arTileNumber;
	QMap<board::TileOrientation, QPixmap> arTileOrientation;

	QSize tileSize;
	QPolygon tilePolygon;
	QSize gridSize;
	QSize cornerSize;
	unsigned int cornerRadius;
	QSize sideSize[3];
	QPolygon sidePolygon[3];

	QPixmap tileOverlay;
	QPixmap cornerOverlay;
	QPixmap sideOverlay[3];

	static const QColor overlayFillColor;
	static const QColor overlayPenColor;

	float distance2(const QPoint & point1, const QPoint & point2) const;
};

#endif /* GRAPHICALMANAGER_H_ */
